#!/bin/bash
# The MIT License (MIT)
#
# Copyright (c) 2014 Steven Penny
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

function usage {
  local rw=(
  'apt-msys2 [command] [packages]'
  ''
  'Commands:'
  '   install <packages>     install packages'
  '   listfiles <packages>   list files owned by packages'
  '   search <patterns>      search for a filename from all available packages'
  )
  printf '%s\n' "${rw[@]}"
}

function urlencode {
  sed '
  s / %2f g
  s : %3a g
  ' <<< "$1"
}

function findworkspace {
  mirror=http://downloads.sourceforge.net/project/msys2
  repo=REPOS/MINGW/$ARCH
  local mirrordir=/var/cache/$(urlencode $mirror)/$repo
  mkdir -p $mirrordir
  cd $mirrordir
}

function getdb {
  [ -e mingw64.files.tar.gz ] && return
  wget $mirror/$repo/mingw64.files.tar.gz
  tar --no-an -Oxf mingw64.files.tar.gz desc files > mingw64.lst
}

function apt-listfiles {
  findworkspace
  getdb
  local pkg
  for pkg in ${packages[*]}
  do
    awk '
    $2 == "%NAME%\n" pkg {
      print $NF
    }
    ' pkg=$pkg RS=%FILENAME% FS='\n\n' mingw64.lst
  done |
  head -c-1
}

function apt-install {
  findworkspace
  getdb
  local pkg file eu oe rw
  for pkg in ${packages[*]}
  do
    file=$(awk '$5 == pkg, $0 = $2' pkg=$pkg RS=%FILENAME% FS='\n' mingw64.lst)
    wget -nc $mirror/$repo/$file
    tar --str 1 -x -C / -f $file

    apt-msys2 listfiles $pkg | grep '\.exe$' | while read eu
    do
      oe=$(sed 's-^.*/--; s-\.exe$--' <<< $eu)
      rw=(
        'tg=$(cygpath -m $1)'
        'shift'
        'cygstart ${0#/usr/local} $tg $*'
      )
      printf '%s\n' "${rw[@]}" > $oe
      install $oe /usr/local/bin
    done

    echo Package $pkg installed
  done
}

function apt-search {
  findworkspace
  getdb
  for pkg in ${packages[*]}
  do
    awk '
    $0 ~ pkg {
      print $5
    }
    ' pkg="$pkg" RS=%FILENAME% FS='\n' mingw64.lst
  done
}

while (( $# ))
do
  case "$1" in

    install | listfiles | search)
      if [[ $command ]]
      then
        packages+=($1)
      else
        command=$1
      fi
      shift
    ;;

    *)
      packages+=($1)
      shift
    ;;

  esac
done

if [ $HOSTTYPE = x86_64 ]
then
  ARCH=x86_64
else
  ARCH=x86
fi

if type -t apt-$command | grep -q function
then
  apt-$command
else
  usage
fi
